//
//  ShortcutsTableViewController.m
//  DemoApplicationShortcut
//
//  Created by 唐可雄 on 16/12/5.
//  Copyright © 2016年 swustyc. All rights reserved.
//

#import "ShortcutsTableViewController.h"

#import "ShortcutDetailViewController.h"

@interface ShortcutsTableViewController () {
    NSMutableArray *staticShortcuts;
    NSMutableArray *dynamicShortcuts;
}

@end

@implementation ShortcutsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    NSArray *shortcuts = [[NSBundle mainBundle] infoDictionary][@"UIApplicationShortcutItems"];
    staticShortcuts = [[NSMutableArray alloc] init];
    for (NSDictionary *shortcut in shortcuts) {
        NSString *shortcutType = shortcut[@"UIApplicationShortcutItemType"];
        NSString *shortcutTitle = shortcut[@"UIApplicationShortcutItemTitle"];
        NSString *localizedShortcutTitle = [[NSBundle mainBundle] localizedInfoDictionary][shortcutTitle];
        NSString *shortcutSubTitle = shortcut[@"UIApplicationShortcutItemSubtitle"];
        NSString *localizedShortcutSubTitle = [[NSBundle mainBundle] localizedInfoDictionary][shortcutSubTitle];
        
        [staticShortcuts addObject:[[UIApplicationShortcutItem alloc] initWithType:shortcutType localizedTitle:localizedShortcutTitle localizedSubtitle:localizedShortcutSubTitle icon:nil userInfo:nil]];
    }
    
    dynamicShortcuts = [NSMutableArray arrayWithArray:[UIApplication sharedApplication].shortcutItems];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return @"Static";
    } else {
        return @"Dynamic";
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return staticShortcuts.count;
    } else {
        return dynamicShortcuts.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CellID"];
    
    UIApplicationShortcutItem *shortcut = nil;
    
    if (indexPath.section == 0) {
        shortcut = staticShortcuts[indexPath.row];
        
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    } else {
        shortcut = dynamicShortcuts[indexPath.row];
    }
    
    cell.textLabel.text = shortcut.localizedTitle;
    cell.detailTextLabel.text = shortcut.localizedSubtitle;
    
    return cell;
}

#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"ShowShortcutDetail"]) {
        NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
        ShortcutDetailViewController *vc = segue.destinationViewController;
        vc.shortcutItem = dynamicShortcuts[indexPath.row];
    }
}

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender {
    NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
    return indexPath.section > 0;
}

#pragma mark - Actions

- (IBAction)done:(UIStoryboardSegue *)segue {
    NSIndexPath *indexPath = self.tableView.indexPathForSelectedRow;
    ShortcutDetailViewController *vc = segue.sourceViewController;
    // 修改数据源
    dynamicShortcuts[indexPath.row] = vc.shortcutItem;
    // 修改应用的动态shortcuts
    [UIApplication sharedApplication].shortcutItems = dynamicShortcuts;
    
    [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (IBAction)cancel:(UIStoryboardSegue *)segue {
    
}


@end
